<%@ page import="com.project.User" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>修改用户信息</title>
  <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
  <link rel="stylesheet" href="css/editUserInfo.css">
  <script>
    function updateCurrentValue() {
      const fieldSelect = document.querySelector('select[name="field"]');
      const currentValueInput = document.querySelector('input[name="currentValue"]');
      const selectedField = fieldSelect.value;
      let currentValue = "";

      switch (selectedField) {
        case "name":
          currentValue = '<%= session.getAttribute("user") != null ? ((User) session.getAttribute("user")).getName() : "" %>';
          break;
        case "email":
          currentValue = '<%= session.getAttribute("user") != null ? ((User) session.getAttribute("user")).getEmail() : "" %>';
          break;
        case "phone":
          currentValue = '<%= session.getAttribute("user") != null ? ((User) session.getAttribute("user")).getPhone() : "" %>';
          break;
        case "password":
          // 注意：实际应用中不建议显示密码
          currentValue = '<%= session.getAttribute("user") != null ? ((User) session.getAttribute("user")).getPassword() : "" %>';
          break;
      }

      currentValueInput.value = currentValue;
    }
  </script>
</head>
<body>
<%@ include file="userNavbar.jsp" %>

<div class="edit-container">
  <h2 class="text-2xl font-bold text-gray-800 mb-6 text-center">修改用户信息</h2>
  <form action="UpdateUserServlet" method="post">
    <div class="mb-4">
      <label class="block text-gray-700 font-semibold mb-2">选择修改属性
        <select name="field" class="w-full px-4 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-400" required onchange="updateCurrentValue()">
          <option value="name">姓名</option>
          <option value="email">邮箱</option>
          <option value="phone">电话</option>
          <option value="password">密码</option>
        </select>
      </label>
    </div>
    <div class="mb-6">
      <label class="block text-gray-700 font-semibold mb-2">当前值
        <input type="text" name="currentValue" class="w-full px-4 py-2 border rounded-lg" value='<%= session.getAttribute("user") != null ? ((User) session.getAttribute("user")).getName() : "" %>' readonly>
      </label>
    </div>
    <div class="mb-6">
      <label class="block text-gray-700 font-semibold mb-2">新值
        <input type="text" name="newValue" class="w-full px-4 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-400" placeholder="请输入新值" required>
      </label>
    </div>
    <button type="submit" class="w-full btn-submit">提交修改</button>
  </form>
</div>
</body>
</html>